using UnityEngine;
using System.Collections;

public class ColorDropped : MonoBehaviour {
	
	public float reactiveTime;
	public float speed;
	public AxisColorDropped axis;
	private Vector3 directionMove;
	private bool hasMove;
	public static bool switchDirection;
    public Transform currentRoom;

	IEnumerator Start () 
	{
		randomDirection(Random.Range(0,4));
		hasMove = true;
        gameObject.GetComponent<DemoRotate>().enabled = false;
		gameObject.GetComponent<BoxCollider>().enabled = false;
		yield return new WaitForSeconds(reactiveTime);
        gameObject.GetComponent<DemoRotate>().enabled = true;
		gameObject.GetComponent<BoxCollider>().enabled = true;
		hasMove = false;
	}
	
	void randomDirection(int i)
    {
                switch(i)
                {
                        case 0:
                                directionMove = Vector3.up;
                        break;
                        
                        case 1:
                                directionMove = -Vector3.up;
                        break;
                        
                        case 2:
                                directionMove = Vector3.right;
                        break;
                        
                        case 3:
                                directionMove = -Vector3.right;
                        break;
                }       
                
     }
	
	void Update () 
	{
		if(hasMove && !axis.isGrounded && !axis.stop)
		{
			transform.Translate(directionMove*(speed*Time.deltaTime));
		}		
	}

    void OnTriggerEnter(Collider obj)
    {
        if (obj.name == "Floor")
        {
            if (currentRoom != null)
            {
                if (obj.GetComponent<RoomInfo>().Father.name != currentRoom.name)
                {
                    currentRoom = obj.GetComponent<RoomInfo>().Father;
                    transform.parent = null;
                    transform.parent = currentRoom;
                    renderer.material.color = new Color(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, obj.renderer.material.color.a);
                }
            }
            else
            {
                currentRoom = obj.GetComponent<RoomInfo>().Father;
                transform.parent = null;
                transform.parent = currentRoom;
                renderer.material.color = new Color(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, 1);    
            }
        }
    }
}
